package com.shuorikeji.huohuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.bean.DjDetailVideoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.jzvideo.JZMediaExo;
import com.shuorikeji.huohuo.view.CustomJzvdStd;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import cn.jzvd.JZUtils;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class DjVideoAdapter extends RecyclerView.Adapter<DjVideoAdapter.BaseViewHolder> {
    private final Context mContext;
    private List<DjDetailVideoBean> mData = new ArrayList<>();
    private ClickCallBack callBack;
    private String djName;
    private int djAllJs;

    public DjVideoAdapter(Context context, ClickCallBack callBackFlag) {
        this.mContext = context;
        this.callBack = callBackFlag;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dj_video_item_layout, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvTitle.setText(mData.get(position).getName());
        holder.tvDzs.setText("0");
        holder.ivDz.setImageResource((mData.get(position).getLove_status() == 1) ? R.mipmap.icon_dj_detail_ydz : R.mipmap.icon_dj_detail_dz);
        holder.ivSc.setImageResource((mData.get(position).getLove_status() == 1) ? R.mipmap.icon_dj_detail_ysc : R.mipmap.icon_dj_detail_sc);
        holder.tvDjName.setText(djName);
        holder.tvInfo.setText(mData.get(position).getName() + "·全" + djAllJs + "集｜" + mData.get(position).getText());
        if (mData.get(position).getIs_lock() == 2) {
            callBack.onClick(7, mData.get(position));
        } else {
            JZUtils.clearSavedProgress(mContext, mData.get(position).getVideourl());
            Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
            holder.jzVideo.setUp(mData.get(position).getVideourl(), "", JzvdStd.STATE_NORMAL, JZMediaExo.class);
            holder.jzVideo.startVideo();
        }
        if (holder.jzVideo.getSnJd() != null) {
            holder.jzVideo.getSnJd().setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                    callBack.onClick(6, holder.jzVideo.getCurrentPositionWhenPlaying() / 60000);
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
        }
        holder.ivBack.setOnClickListener(view -> {
            // 暂停播放
            try {
                if (holder.jzVideo != null) {
                    holder.jzVideo.mediaInterface.pause(); // 核心暂停方法
                    holder.jzVideo.onStatePause(); // 更新UI状态
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            callBack.onClick(2, null);
        });
        holder.ivDz.setOnClickListener(view -> {
            callBack.onClick(3, mData.get(position));
        });
        holder.ivSc.setOnClickListener(view -> {
            callBack.onClick(4, mData.get(position));
        });
        holder.tvXj.setOnClickListener(view -> {
            callBack.onClick(5, mData.get(position));
        });
        holder.tvZkSq.setOnClickListener(view -> {
            if (holder.tvInfo.getMaxLines() == 2) {
                holder.tvInfo.setMaxLines(Integer.MAX_VALUE);
                holder.tvZkSq.setText("收起");
            } else {
                holder.tvInfo.setMaxLines(2);
                holder.tvZkSq.setText("展开");
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onClick(1, mData.get(position));
                }
            }
        });
    }

    public void setData(List<DjDetailVideoBean> data, String djName, int djAllJs) {
        this.djAllJs = djAllJs;
        this.djName = djName;
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        ImageView ivBack;
        TextView tvTitle;
        ImageView ivPlay;
        ImageView ivDz;
        TextView tvDzs;
        ImageView ivSc;
        TextView tvSc;
        TextView tvDjName;
        TextView tvInfo;
        TextView tvZkSq;
        TextView tvXj;
        CustomJzvdStd jzVideo;

        public BaseViewHolder(View itemView) {
            super(itemView);
            ivBack = itemView.findViewById(R.id.iv_back);
            tvTitle = itemView.findViewById(R.id.tv_title);
            ivPlay = itemView.findViewById(R.id.iv_play);
            ivDz = itemView.findViewById(R.id.iv_dz);
            tvDzs = itemView.findViewById(R.id.tv_dzs);
            ivSc = itemView.findViewById(R.id.iv_sc);
            tvSc = itemView.findViewById(R.id.tv_sc);
            tvDjName = itemView.findViewById(R.id.tv_dj_name);
            tvInfo = itemView.findViewById(R.id.tv_info);
            tvZkSq = itemView.findViewById(R.id.tv_zk_sq);
            tvXj = itemView.findViewById(R.id.tv_xj);
            jzVideo = itemView.findViewById(R.id.jz_video);
        }
    }
}
